package com.ang.jdk8.stream;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ssang
 * @date 2022-03-03 14:25
 */
public class CollectorsTest {

    public static void main(String[] args) {
        CollectorsTest test = new CollectorsTest();
        test.test1();
    }

    /**
     * List转Map
     */
    public void test1(){
        User u1 = new User(1,"a1");
        User u2 = new User(2,"a1");
        User u3 = new User(3,"a1");

        List<User> list = new ArrayList<>(Arrays.asList(u1,u2,u3));

        Map<Integer,User> map = list.stream().collect(
            Collectors.toMap(
                User::getId, v->v, (k1,k2)->k1
            )
        );

        //forEach改变原来的list
        list.forEach(l->{
            l.setName("test");
        });
        list.forEach(l->{
            System.out.println(l.getName());
        });
    }


    /**
     * 内部类
     */
    @Data
    @AllArgsConstructor
    public class User{
        private int id;
        private String name;
    }
}
